﻿using System;
using System.Collections.Generic;

/**  版本信息模板在安装目录下，可自行修改。
* StringHelper.cs
*
* 功 能： N/A
* 类 名： Product
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2013-4-17 09:50:42   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System.Linq;

namespace CommonControl
{
    /// <summary>
    /// 字符串帮组类
    /// </summary>
    public class StringHelper
    {
        private class LetterItem
        {
            private String fLetter;
            private Int64 fMinValue;
            private Int64 fMaxValue;
            public String Letter
            {
                get
                {
                    return fLetter;
                }
            }
            public Int64 MinValue
            {
                get
                {
                    return fMinValue;
                }
            }
            public Int64 MaxValue
            {
                get
                {
                    return fMaxValue;
                }
            }
            public LetterItem(String fLetter, Int64 fMinValue, Int64 fMaxValue)
            {
                this.fLetter = fLetter;
                this.fMinValue = fMinValue;
                this.fMaxValue = fMaxValue;
            }
        }
        private static List<LetterItem> dictionaryLetter;

        static StringHelper()
        {
            dictionaryLetter = new List<LetterItem>();
            dictionaryLetter.Add(new LetterItem("A", 45217, 45252));
            dictionaryLetter.Add(new LetterItem("B", 45253, 45760));
            dictionaryLetter.Add(new LetterItem("C", 45761, 46317));
            dictionaryLetter.Add(new LetterItem("D", 46318, 46825));
            dictionaryLetter.Add(new LetterItem("E", 46826, 47009));
            dictionaryLetter.Add(new LetterItem("F", 47010, 47296));
            dictionaryLetter.Add(new LetterItem("G", 47297, 47613));
            dictionaryLetter.Add(new LetterItem("H", 47614, 48118));
            dictionaryLetter.Add(new LetterItem("J", 48119, 49061));
            dictionaryLetter.Add(new LetterItem("K", 49062, 49323));
            dictionaryLetter.Add(new LetterItem("L", 49324, 49895));
            dictionaryLetter.Add(new LetterItem("M", 49896, 50370));
            dictionaryLetter.Add(new LetterItem("N", 50371, 50613));
            dictionaryLetter.Add(new LetterItem("O", 50614, 50621));
            dictionaryLetter.Add(new LetterItem("P", 50622, 50905));
            dictionaryLetter.Add(new LetterItem("Q", 50906, 51386));
            dictionaryLetter.Add(new LetterItem("R", 51387, 51445));
            dictionaryLetter.Add(new LetterItem("S", 51446, 52217));
            dictionaryLetter.Add(new LetterItem("T", 52218, 52697));
            dictionaryLetter.Add(new LetterItem("W", 52698, 52979));
            dictionaryLetter.Add(new LetterItem("X", 52980, 53640));
            dictionaryLetter.Add(new LetterItem("Y", 53689, 54480));
            dictionaryLetter.Add(new LetterItem("Z", 54481, 55289));
        }

        /// <summary>  
        /// 获取一段中文中每个中文拼音的第一个字母  
        /// </summary>
        /// <param name="fInputChinese">需要获取字母的中文</param>
        /// <returns>中文拼音的第一个字母</returns>
        public static string GetFirstLetterOfChinese(string fInputChinese)
        {
            return GetFirstLetterOfChinese(fInputChinese, false);
        }

        /// <summary>  
        /// 获取一段中文中每个中文拼音的第一个字母  
        /// </summary>
        /// <param name="fInputChinese">需要获取字母的中文</param>
        /// <param name="fReutrnEmptyWhenFailure">当输入不是中文时是否返回空值。True:返回空值；False：返回传入参数的大写</param>
        /// <returns>中文拼音的第一个字母</returns>
        public static string GetFirstLetterOfChinese(string fInputChinese, Boolean fReutrnEmptyWhenFailure)
        {
            var letters = string.Empty;

            foreach (char c in fInputChinese.ToCharArray())
            {
                letters += GetFirstLetterOfPinyin(c.ToString(), fReutrnEmptyWhenFailure);
            }
            return letters;
        }

        /// <summary>  
        /// 获取一个中文拼音的第一个字母。  
        /// </summary>
        /// <param name="fInputSingleChinese">需要获取字母的一个中文</param>
        /// <param name="fReutrnEmptyWhenFailure">当输入不是中文时是否返回空值。True:返回空值；False：返回传入参数的大写</param>
        /// <returns>中文拼音的第一个字母</returns>
        private static string GetFirstLetterOfPinyin(String fInputSingleChinese, Boolean fReutrnEmptyWhenFailure)
        {
            var byteArray = System.Text.Encoding.Default.GetBytes(fInputSingleChinese);
            if (byteArray.Length == 1)
            {
                return fReutrnEmptyWhenFailure
                    ? fInputSingleChinese.ToUpper()
                    : String.Empty;
            }
            var minValue = (short)(byteArray[0]);
            var maxValue = (short)(byteArray[1]);
            var value = minValue * 256 + maxValue;
            foreach (LetterItem letterItem in dictionaryLetter)
            {
                if (value >= letterItem.MinValue &&
                    value <= letterItem.MaxValue)
                {
                    return letterItem.Letter;
                }
            }
            return "?";
        }
    }
}
